import java.awt.event.MouseEvent;


public class IA implements IPlayer
{
	public static final int INFINITY = Integer.MAX_VALUE;
	public static final int DEPTH=6;
	private Grid grid;

    public IA(Grid g)
    {
    	grid=g;
    }
    
    public int play()
    {
    	Grid g=new Grid(grid);
    	Int colonne=new Int(0);
    	return negamax(g, DEPTH, colonne, 1);
    }
        
    private int negamax(Grid g, int depth, Int colonne, int color)
	{
    	int value,bestvalue=-INFINITY;

    	if(g.won()){return -INFINITY;}
    	
    	if(g.full()){return 0;}
	
    	if(depth == 0){return color*g.evaluation();}
    	for(int i=0; i<P4.WIDTH; i++)
		{
    		if(g.put(i)!=P4.COLONNE_NON_VALIDE)
    		{
	    		g.switchJoueur();
				value = -negamax(g, depth-1, colonne, -color);
				g.switchJoueur();
				g.remove(i);
				if(value>bestvalue)
				{
					bestvalue=value;
					colonne.setVal(i);
				}
			}
		}
    	if(depth==DEPTH)
    		return colonne.getVal();
    	else
    		return bestvalue;
	}
    
    

    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
}

